using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy3DRoller : EnemyBaseFixed
{
    private Vector2 rollDirection;
    private Sprite[] sprites;
    private float[] rollerDistance;
    private SeqAction middleEffectAction=new(){};
    private Sprite[] middleSprtes;
    private Vector2[] collidersOffset;
    private Vector2[] collidersSize;
    private SpriteRenderer middleEffectSR;
    private BoxCollider2D boxCollider2D;

    private Vector2 targetPosition;
    public float runOutTime=146f/60f;
    private float bottomLimit;
    void Awake()
    {
        base.Init();
        middleEffectSR=transform.Find("中-滚动效果").GetComponent<SpriteRenderer>();
        boxCollider2D=GetComponent<BoxCollider2D>();
        targetPosition=camera3DLevel.TransformEnemyViewToPlayerView(transform.position);
        rollDirection=targetPosition-new Vector2(transform.position.x,transform.position.y);
        rollDirection.Normalize();
        rollerDistance=new float[]{
            Camera.main.ViewportToScreenPoint(new Vector3(1,1,0)).y,
            // 116f/16f,
            102/16f,
            87/16f,
            71/16f,
        };
        sprites=new Sprite[]{
            ResourcesPool.Sprites["魂-滚筒-最小"],
            ResourcesPool.Sprites["魂-滚筒-小"],
            ResourcesPool.Sprites["魂-滚筒-中"],
            ResourcesPool.Sprites["魂-滚筒-大"],
        };
        middleSprtes=new Sprite[]{
            ResourcesPool.Sprites["魂-滚筒-中1"],
            ResourcesPool.Sprites["魂-滚筒-中2"],
            ResourcesPool.Sprites["魂-滚筒-中3"],
        };
        collidersOffset=new Vector2[]{
            new(0f,0.125f),
            new(0f,0.1875f),
            new(0f,0.21875f),
            new(0f,0.25f),
        };
        collidersSize=new Vector2[]{
            new(0.625f,0.25f),
            new(1f,0.375f),
            new(1.3125f,0.4375f),
            new(1.5f,0.5f),
        };
        mainSR.sprite=sprites[0];
        boxCollider2D.size=collidersSize[0];
        bottomLimit=camera3DLevel.GetPlayerViewRect().min.y/16f-0.5f;
    }

    void FixedUpdate()
    {
        base.CommonFixedUpdate();
    }

    protected void UpdateStateNone(bool stateChanged, EnumState inState, out EnumState outState)
    {
        outState = EnumState.Working;

    }
    protected void UpdateStateWorking(bool stateChanged, EnumState inState, out EnumState outState)
    {
        outState = inState;
        if(stateChanged){
            float distance=Vector2.Distance(targetPosition,transform.position);
            rig.velocity=rollDirection*(distance/runOutTime);
        }
        Vector2 position=transform.position;
        middleEffectSR.enabled=false;
        for(int i=0;i<rollerDistance.Length;i++){
            float distance=rollerDistance[i];
            if(position.y<=distance){
                mainSR.sprite=sprites[i];
                boxCollider2D.offset=collidersOffset[i];
                boxCollider2D.size=collidersSize[i];
            }else{
                if(i==3){
                    middleEffectSR.enabled=true;
                    middleEffectAction.Update((SeqAction seq)=>{
                        int index=Mathf.FloorToInt(seq.DurationTime/(10f/60f))%middleSprtes.Length;
                        middleEffectSR.sprite=middleSprtes[index];
                    });
                }
                break;
            }
        }
    }

    protected override void UpdateStateDie(bool stateChanged, EnumState inState, out EnumState outState)
    {
        outState=inState;
        Instantiate(Constant.Prefabs.EnemyBoom1.Load(),transform.position,Quaternion.identity);
        Destroy(gameObject);
    }

    protected override bool InViewPort(float expandSize = 0.0f){
        bool ret=transform.position.y>bottomLimit;
        return ret;
    }


}
